/**
 * Created with IntelliJ IDEA.
 * User: Evgeniy Gusar
 * Date: 24.05.13
 * Time: 5:30
 * To change this template use File | Settings | File Templates.
 */
import java.util.Scanner;
import java.util.Formatter;

class Division {
    public static void main(String [] args) {
        Division div = new Division();

        Scanner sc = new Scanner(System.in);
        System.out.print("Dividend: ");
        int dividend = sc.nextInt();
        System.out.print("Divisor: ");
        int divisor = sc.nextInt();
        div.drawingColumn(dividend, divisor);
        div.workDivision(dividend, divisor);
    }

    void drawingColumn (int dividend, int divisor) {
        Formatter f = new Formatter();
        f.format("%n%s | %s", dividend, divisor);
        System.out.println(f);

        drawLine(dividend); System.out.print("-|-"); drawLine(divisor);
        System.out.println("");
    }

    int workDivision(int dividend, int divisor) {
        int d = dividend;
        int i = 0;
        int[] digits = new int[10];
        while (d > 0) {
            digits[i++] = d%10;
            d /=10;
        }

        int result=0;
        int gaps = 0;
        int buf = 0;
        int z = digits[i-1];
        int resNextDig = z/divisor;

        for (int u = i-1; u >= 0; u--) {
            resNextDig = z/divisor;
            if (resNextDig == 0)  {
                z = z*10 + digits[u-1];
            }

            else if (resNextDig > 0) {
                result += resNextDig*Math.pow(10, u);

                drawBlanks(gaps); System.out.println(z);
                drawBlanks(gaps + (numOfDigits(z) - numOfDigits(divisor * resNextDig)));
                System.out.println(divisor*resNextDig);
                drawBlanks(gaps); drawLine(z);

                System.out.println("");
                buf = z;
                z = z - divisor*(resNextDig);

                gaps += numOfDigits(buf) - numOfDigits(z);

                if (u!=0)
                    z = z *10 + digits[u-1];
            }
        }

        drawBlanks(gaps); System.out.println(z);
        System.out.println("The result of long division: " + result);
        return result;
    }
	
	/**
     * Output of lines depending on the number of digits in a number
     */

    void drawLine(int a) {
        for (int i = 0; i<numOfDigits(a); i++){
            System.out.print("-");
        }
    }
	
	/**
     * Method for calculating the amount of digits in a number
     */

    int numOfDigits(int number) {
        if (number == 0)
            return 1;
        int k = 1;
        for(int gaps = number; gaps>0; gaps/=10) {
            k++;
        }
        return k-1;
    }
	
	/**
     * Output of blanks
     */
	
    void drawBlanks(int p) {
        for(int v = 0; v<p; v++)
            System.out.print(" ");
    }
}